<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">发送ajax请求</button>
    <script>
        //绑定元素
        var btn = document.querySelector('#btn');
        //绑定点击事件
        btn.addEventListener('click', () => {
            //创建ajax对象
            var xhr = new XMLHttpRequest();
            //告诉ajax对象发送方式，请求地址
            xhr.open('get', 'http://localhost:3000/error');
            //发送请求
            xhr.send();
            //获取服务器端响应到客户端的数据
            xhr.onload = () => {
                //xhr.status获取http状态码
                console.log(xhr.responseText);
                console.log(xhr.status);
            }
        })
    </script>
</body>

</html>